﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;

namespace AddIn.EA.Core.Common
{
    public class AddInRegistryResources
    {
        #region Constantes
        /// <summary>
        /// Chave de registro do add-in no EA
        /// </summary>
        public const string AddInEAKey = @"Software\Sparx Systems\EAAddins\Add-In for Architecture";

        /// <summary>
        /// Chave para o diretório da aplicação
        /// </summary>
        public const string BaseDirectoryKey = "BaseDirectory";

        /// <summary>
        /// Chave para carregar o assembly no reference do Visual Studio
        /// </summary>
        public const string FrameworkKey = @"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\AddInArquitetura.Framework";

        /// <summary>
        /// Chave do menu
        /// </summary>
        public const string MenuKey = "Menu";

        /// <summary>
        /// Chave para o nome do menu
        /// </summary>
        public const string MenuNameKey = "MenuName";

        /// <summary>
        /// Chave para schema
        /// </summary>
        public const string SchemaKey = "Schema";

        /// <summary>
        /// Chave de registro para chaves do programa
        /// </summary>
        public const string SubKey = @"SOFTWARE\AddInArchitecture";

        #endregion

        #region Methods
        public static string GetBaseDirectory()
        {
            RegistryKey subKey = Registry.LocalMachine.CreateSubKey(AddInRegistryResources.SubKey);
            return subKey.GetValue(AddInRegistryResources.BaseDirectoryKey).ToString();
        }

        public static string GetApplicationFolder()
        {
            string appFolder = Path.Combine(GetBaseDirectory(), "AddIn");

            if (!Directory.Exists(appFolder))
                Directory.CreateDirectory(appFolder);

            return appFolder;
        }

        public static string GetSchemaFile()
        {
            RegistryKey subKey = Registry.LocalMachine.CreateSubKey(AddInRegistryResources.SubKey);
            string schemaFile = subKey.GetValue(AddInRegistryResources.SchemaKey).ToString();

            if (!File.Exists(schemaFile))
                schemaFile = Path.Combine(GetBaseDirectory(), schemaFile);

            return schemaFile;
        }


        public static string GetMenuFolder()
        {
            RegistryKey subKey = Registry.LocalMachine.CreateSubKey(AddInRegistryResources.SubKey);
            string menuFolder = subKey.GetValue(AddInRegistryResources.MenuKey).ToString();

            if (!Directory.Exists(menuFolder))
                menuFolder = Path.Combine(GetBaseDirectory(), menuFolder);

            if (!Directory.Exists(menuFolder))
                Directory.CreateDirectory(menuFolder);

            return menuFolder;
        }

        public static string GetMenuName()
        {
            RegistryKey subKey = Registry.LocalMachine.CreateSubKey(AddInRegistryResources.SubKey);
            return subKey.GetValue(AddInRegistryResources.MenuNameKey).ToString();
        }
        #endregion
    }
}
